package com.woniu.model.gh.utils;

import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

//自定义分页
@Data
@NoArgsConstructor
public class PageVo<T> {

    //分页数据
    private List<T> list;

    //页码总数
    private Long pageCount;

    //数据总数
    private Long dataTotal;

    //当前页码
    private Long pageNum;

    //当前页大小
    private Long pageSize;

    ///构造函数
    public PageVo(Integer pageNum, Integer pageSize, List<T> list) {
        getData(pageNum, pageSize, list);
    }

    //获取分页数据
    public void getData(Integer pageNum, Integer pageSize, List<T> list) {

        //获取页码和页大小
        this.pageNum = pageNum.longValue();
        this.pageSize = pageSize.longValue();

        //获取数据总数
        Integer listSize = list.size();
        this.dataTotal = listSize.longValue();

        //获取总页数
        if (listSize % pageSize == 0)
            this.pageCount = listSize.longValue() / pageSize.longValue();
        else
            this.pageCount = listSize.longValue() / pageSize.longValue() + 1;

        //获取分页集合
        int number = (pageNum - 1) * pageSize;
        List<T> pageList = new ArrayList<>();
        for (int i = number; i < number + pageSize; i++) {
            if (i >= dataTotal) break;
            pageList.add(list.get(i));
        }
        this.list = pageList;
    }

}
